﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using wdsDeApp04.Models;

namespace wdsDeApp04.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return View(new Member());
        }

        [HttpPost]
        public ActionResult Index(string name = null, string pwd = null)
        {
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(pwd))
            {
                using (Model1 model = new Model1())
                {
                    var userInfo = model.Member.Where(u => u.UserName == name).FirstOrDefault(u => u.Password == pwd);
                    if (userInfo != null)
                    {
                        Session["UserId"] = userInfo.Id;
                        return Json(1);
                    }
                    return Json(0);
                }
            }
            return Json(2);
        }

        [HttpGet]
        public ActionResult List(string movieName = null, string director = null)
        {
            using (Model1 model = new Model1())
            {
                #region 获取所有影评信息
                var allInfo = model.FilmReview.Where(u => true).ToList();
                if (!string.IsNullOrEmpty(movieName))
                    allInfo = allInfo.Where(u => u.MovieName.IndexOf(movieName) >= 0).ToList();
                if (!string.IsNullOrEmpty(director))
                    allInfo = allInfo.Where(u => u.Director.IndexOf(director) >= 0).ToList();
                #endregion

                #region 获取更换列表的信息
                var allBer = model.Member.Where(u => true).ToList();
                Dictionary<int, string> nameDic = new Dictionary<int, string>();
                foreach (var item in allBer)
                    nameDic.Add(item.Id, item.Name);
                Dictionary<int, string> sexDic = new Dictionary<int, string>();
                foreach (var item in allBer)
                    sexDic.Add(item.Id, item.Sex ? "男" : "女");
                ViewBag.nameDic = nameDic;
                ViewBag.sexDic = sexDic;
                #endregion

                return View(allInfo);
            }
        }

        [HttpGet]
        public ActionResult Add()
        {
            if (Session["UserId"] == null)
                return RedirectToAction("Index", "MyHome");

            return View();
        }

        [HttpPost]
        public ActionResult Add(FilmReview filmReview)
        {
            if (Session["UserId"] != null)
            {
                filmReview.MemberId = Convert.ToInt32(Session["UserId"]);

                using (Model1 model = new Model1())
                {
                    model.FilmReview.Add(filmReview);
                    if (model.SaveChanges() == 1)
                        return RedirectToAction("List", "MyHome");
                    else
                        return Json(false);
                }
            }
            return RedirectToAction("Index", "MyHome");
        }
    }
}